package kylin.service.impl;

import java.util.List;

import kylin.dao.DocInterfaceMethodDao;
import kylin.dao.DocInterfaceParamsDao;
import kylin.entity.DocInterfaceMethod;
import kylin.entity.DocInterfaceParams;
import kylin.service.DocInterfaceMethodService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * <p>Class Name : DocInterfaceMethodServiceImpl</p>
 * <p>Class Desc : 处理接口文档方法的业务逻辑层</p>
 * <p>Date : 2015-11-5 下午1:45:07</p>
 * <p>Author : XQL</p>
 */
@Service
public class DocInterfaceMethodServiceImpl implements DocInterfaceMethodService {

	@Autowired
	private DocInterfaceMethodDao docInterfaceMethodDao;

	@Autowired
	private DocInterfaceParamsDao docInterfaceParamsDao;

	public DocInterfaceParamsDao getDocInterfaceParamsDao() {
		return docInterfaceParamsDao;
	}

	public void setDocInterfaceParamsDao(DocInterfaceParamsDao docInterfaceParamsDao) {
		this.docInterfaceParamsDao = docInterfaceParamsDao;
	}

	public DocInterfaceMethodDao getDocInterfaceMethodDao() {
		return docInterfaceMethodDao;
	}

	public void setDocInterfaceMethodDao(DocInterfaceMethodDao docInterfaceMethodDao) {
		this.docInterfaceMethodDao = docInterfaceMethodDao;
	}

	/**
	 * <p>Method Name : saveMethodAndParams</p>
	 * <p>Method Desc : 保存接口文档方法并且保存方法参数</p>
	 * <p>Date : 2015-11-5 下午11:37:33</p>
	 * <p>Author : XQL</p>
	 * @param docMethod
	 */
	public void saveMethodAndParams(DocInterfaceMethod docMethod){

		//docMethod.setDemo(docMethod.getDemo().replaceAll("\"\"", "\""));
		if(docMethod.getId() != 0){
			if(docInterfaceParamsDao.deleteByMethod(docMethod.getId())){
				docInterfaceMethodDao.deleteById(docMethod.getId());
			}
		}

		docInterfaceMethodDao.insert(docMethod);

		for(DocInterfaceParams docParams : docMethod.getParamsList()){
			//if(docParams.getId() !=0 && "".equals(docParams.getName())){
			//	docInterfaceParamsDao.deleteById(docParams.getId());
			//}
			if(!"".equals(docParams.getName())){
				docParams.setDocInterfaceMethod(docMethod);
				docInterfaceParamsDao.insert(docParams);
			}
		}
	}

	/**
	 * <p>Method Name : giveMethodList</p>
	 * <p>Method Desc : 根据接口文档ID查询所有接口方法</p>
	 * <p>Date : 2015-11-6 上午12:39:29</p>
	 * <p>Author : XQL</p>
	 * @param docInterface_id 接口文档ID
	 * @return
	 */
	public List<DocInterfaceMethod> giveMethodList(int docInterface_id){
		List<DocInterfaceMethod> list = docInterfaceMethodDao.query(docInterface_id);
		for(DocInterfaceMethod docMethod : list){
			//docMethod.setDocInterface(null);
			List<DocInterfaceParams> paramsList = docInterfaceParamsDao.query(docMethod.getId());
			for(DocInterfaceParams params : paramsList){
				params.setDocInterfaceMethod(null);
			}
			docMethod.setParamsList(paramsList);
		}
		return list;
	}

	/**
	 * <p>Method Name : giveSearchMethod</p>
	 * <p>Method Desc : 根据接口文档ID和方法名称，查询所有接口方法[索引方式]</p>
	 * <p>Date : 2015-11-7 上午12:04:23</p>
	 * <p>Author : XQL</p>
	 * @param docInterface_id
	 * @param searchStr
	 * @return
	 */
	public List<DocInterfaceMethod> giveSearchMethod(int docInterface_id,String searchStr){
		return docInterfaceMethodDao.queryByLike(docInterface_id, searchStr);
	}

	/**
	 * <p>Method Name : giveById</p>
	 * <p>Method Desc : 根据主键ID查询接口方法</p>
	 * <p>Date : 2015-11-6 下午9:36:58</p>
	 * <p>Author : XQL</p>
	 * @param docMethod_id 接口方法ID
	 * @return
	 */
	public DocInterfaceMethod giveById(int docMethod_id){
		DocInterfaceMethod docMethod = docInterfaceMethodDao.get(docMethod_id);
		if(docMethod != null){
			List<DocInterfaceParams> paramsList = docInterfaceParamsDao.query(docMethod.getId());
			for(DocInterfaceParams params : paramsList){
				params.setDocInterfaceMethod(null);
			}
			docMethod.setParamsList(paramsList);
		}
		return docMethod;
	}

}
